package cn.iocoder.yudao.module.ai.controller.admin.materialreleasefile;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.ai.controller.admin.materialreleasefile.vo.*;
import cn.iocoder.yudao.module.ai.dal.dataobject.materialreleasefile.MaterialReleaseFileDO;
import cn.iocoder.yudao.module.ai.service.materialreleasefile.MaterialReleaseFileService;

@Tag(name = "管理后台 - 素材解压缩文件列")
@RestController
@RequestMapping("/ai/material-release-file")
@Validated
public class MaterialReleaseFileController {

    @Resource
    private MaterialReleaseFileService materialReleaseFileService;

    @PostMapping("/create")
    @Operation(summary = "创建素材解压缩文件列")
    @PreAuthorize("@ss.hasPermission('ai:material-release-file:create')")
    public CommonResult<Long> createMaterialReleaseFile(@Valid @RequestBody MaterialReleaseFileSaveReqVO createReqVO) {
        return success(materialReleaseFileService.createMaterialReleaseFile(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新素材解压缩文件列")
    @PreAuthorize("@ss.hasPermission('ai:material-release-file:update')")
    public CommonResult<Boolean> updateMaterialReleaseFile(@Valid @RequestBody MaterialReleaseFileSaveReqVO updateReqVO) {
        materialReleaseFileService.updateMaterialReleaseFile(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除素材解压缩文件列")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ai:material-release-file:delete')")
    public CommonResult<Boolean> deleteMaterialReleaseFile(@RequestParam("id") Long id) {
        materialReleaseFileService.deleteMaterialReleaseFile(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得素材解压缩文件列")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ai:material-release-file:query')")
    public CommonResult<MaterialReleaseFileRespVO> getMaterialReleaseFile(@RequestParam("id") Long id) {
        MaterialReleaseFileDO materialReleaseFile = materialReleaseFileService.getMaterialReleaseFile(id);
        return success(BeanUtils.toBean(materialReleaseFile, MaterialReleaseFileRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得素材解压缩文件列分页")
    @PreAuthorize("@ss.hasPermission('ai:material-release-file:query')")
    public CommonResult<PageResult<MaterialReleaseFileRespVO>> getMaterialReleaseFilePage(@Valid MaterialReleaseFilePageReqVO pageReqVO) {
        PageResult<MaterialReleaseFileDO> pageResult = materialReleaseFileService.getMaterialReleaseFilePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, MaterialReleaseFileRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出素材解压缩文件列 Excel")
    @PreAuthorize("@ss.hasPermission('ai:material-release-file:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMaterialReleaseFileExcel(@Valid MaterialReleaseFilePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MaterialReleaseFileDO> list = materialReleaseFileService.getMaterialReleaseFilePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "素材解压缩文件列.xls", "数据", MaterialReleaseFileRespVO.class,
                        BeanUtils.toBean(list, MaterialReleaseFileRespVO.class));
    }

}